package com.ny.zmb.openapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.ny.zmb.openapi.config.YsConfig;
import com.ny.zmb.openapi.controller.entity.OrdersOnline;
import com.ny.zmb.openapi.controller.entity.OrdersRefund;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.util.*;
import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RequestMapping("refund")
@RestController
public class RefundController {


    @Resource
    private YsUtil ycutil;

    /**
     * 商户端私钥证书
     * 生产环境需要使用自己生产的私钥证书
     */
    private String YS_PUBLIC_CER_PATH = new YsConfig().getYsPublicCerPath();


    /**
     * 请求地址域名
     */
    private static final String HTTP_NET = "https://appdev.ysepay.com";

    /**
     * 订单总额分账登记
     */
    @PostMapping("divRegist")
    public Result divRegist(@RequestBody OrdersRefund ordersRefund) throws Exception{
        String httpUrl = HTTP_NET+"/openapi/unify/trade/refund";
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("origOrderId",ordersRefund.getOrigOrderId());
        dataMap.put("origTradeSn",ordersRefund.getOrigTradeSn());
        dataMap.put("shopDate",ordersRefund.getShopDate());
        dataMap.put("refundReason",ordersRefund.getRefundAmount());
        dataMap.put("refundOrderId",ordersRefund.getRefundOrderId());
        dataMap.put("notifyUrl",ordersRefund.getNotifyUrl());
        Map<String,String> reqMap = ycutil.resultMap("refundAmount",ordersRefund.getRefundAmount(),dataMap,"unify.trade.refund");
        String key = reqMap.get("resultKey");
        reqMap.remove("resultKey");
        /** 7、发送http请求获取返回结果，请求地址以文档为准*/
        try {
            String result = HttpRequestUtil.getDoPostResponse(httpUrl, reqMap, "UTF-8", null, 20*1000);
            byte[] res = Base64.decodeBase64(result);
            Map<String, String> resMap = (Map<String, String>) JSONObject.parse(new String(res,"UTF-8"));
            System.out.println("http请求返回的结果为:"+JSONObject.toJSONString(resMap));

            /** 8、对结果进行解密，并使用银盛公钥验签*/
            if(StringUtils.isNotBlank(resMap.get("sign"))) {
                byte[] srcData = GMSignUtils.getSignDataStr1(resMap).getBytes("UTF-8");
                GMCertInfo verifyCertInfo = GMSignUtils.getVerifyCertInfo(YS_PUBLIC_CER_PATH);
                boolean validateSignResult = GMSignUtils.verifyMsgSignSM2(verifyCertInfo,Base64.decodeBase64(resMap.get("sign")),srcData);
                if(!validateSignResult){
                    System.out.println("验签失败");
                    return Result.fail("签名验证失败");
                }
            }else{
                System.out.println("验签失败,未返回加签信息,可能是未配置发起方或者未配置发起方的证书类型,返回结果提示为:{}"+resMap.get("msg"));
                return Result.fail(resMap.get("msg"));
            }

            /** 9、针对自己的业务，根据返回的结果的code 和 subCode 做接入方的业务处理*/
            /** 10、使用上面生成的加密密钥key，解密返回的业务参数*/
            if(StringUtils.isNotBlank(resMap.get("businessData"))) {
                byte[] data_ = Base64.decodeBase64(resMap.get("businessData"));
                byte[] data = AESUtil.decrypt(data_, ByteUtil.hexStringToBytes(key));
                String s = new String(data, "UTF-8");
                resMap.put("businessData", s);
                System.out.println("解密后的业务参数:"+new String(data,"UTF-8"));
                return Result.ok(JSONObject.parseObject(s));
            }
            return Result.fail("操作失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }
}
